package cn.gz.dzy.core.controller.api;


import cn.gz.dzy.base.util.JwtUtils;
import cn.gz.dzy.common.exception.Assert;
import cn.gz.dzy.common.result.R;
import cn.gz.dzy.common.result.ResponseEnum;
import cn.gz.dzy.common.util.RegexValidateUtils;
import cn.gz.dzy.core.pojo.vo.LoginVO;
import cn.gz.dzy.core.pojo.vo.RegisterVO;
import cn.gz.dzy.core.pojo.vo.UserInfoVO;
import cn.gz.dzy.core.service.UserInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 用户基本信息 前端控制器
 * </p>
 *
 * @author dzy
 * @since 2021-11-04
 */
@Api(tags = "会员接口")
@RestController
@RequestMapping("/api/core/userInfo")
@Slf4j
//@CrossOrigin
public class ApiUserInfoController {
    @Resource
    private RedisTemplate redisTemplate;

    @Resource
    private UserInfoService userInfoService;


    @ApiOperation("会员注册")
    @PostMapping("/register")
    public R register(
            @RequestBody RegisterVO registerVO){
        String mobile = registerVO.getMobile();
        String password = registerVO.getPassword();
        String code = registerVO.getCode();

        Assert.notEmpty(mobile, ResponseEnum.MOBILE_NULL_ERROR);
        Assert.notEmpty(password, ResponseEnum.PASSWORD_NULL_ERROR);
        Assert.notEmpty(code, ResponseEnum.CODE_NULL_ERROR);
        Assert.isTrue(RegexValidateUtils.checkCellphone(mobile), ResponseEnum.MOBILE_ERROR);

        //判断验证码是否正确

        String codeGen = (String)redisTemplate.opsForValue().get("srb:sms:code:" + mobile);
        //CODE_ERROR(-206, "验证码不正确"),
        System.out.println("RegisterVO:"+registerVO.toString());
        System.out.println("验证码key:"+"srb:sms:code:" + mobile);

        System.out.println("code"+code);
        System.out.println("codeGen"+codeGen);

        Assert.equals(code, codeGen, ResponseEnum.CODE_ERROR);
        //注册
        userInfoService.register(registerVO);
        return R.ok().message("注册成功");
    }

    @ApiOperation("会员登录")
    @PostMapping("/login")
    public R login(@RequestBody LoginVO loginVO, HttpServletRequest request){
        String mobile = loginVO.getMobile();
        String password = loginVO.getPassword();
        Assert.notEmpty(mobile, ResponseEnum.MOBILE_NULL_ERROR);
        Assert.notEmpty(password, ResponseEnum.PASSWORD_NULL_ERROR);
        String ip = request.getRemoteAddr();

        UserInfoVO userInfoVO =userInfoService.login(loginVO,ip);
        return R.ok().data("userInfo", userInfoVO);
    }

    @ApiOperation("校验令牌")
    @GetMapping("/checkToken")
    public R checkToken(HttpServletRequest request) {
        String token = request.getHeader("token");
        boolean b = JwtUtils.checkToken(token);
        if(b){
            return R.ok();
        }else{
            //LOGIN_AUTH_ERROR(-211, "未登录"),
            return R.setResult(ResponseEnum.LOGIN_AUTH_ERROR);
        }
    }
}

